package fule.com.playandroidkotlin.ui.activity

import android.content.Intent
import android.view.View
import androidx.fragment.app.FragmentManager
import butterknife.OnClick
import fule.com.kotlindemo.base.BaseView
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.BaseActivity
import fule.com.playandroidkotlin.base.BasePresenter
import fule.com.playandroidkotlin.ui.fragment.HomeFragment
import fule.com.playandroidkotlin.ui.fragment.KnowledgeFragment
import fule.com.playandroidkotlin.ui.fragment.NavigationFragment
import fule.com.playandroidkotlin.ui.fragment.ProjectFragment
import fule.com.playandroidkotlin.ui.util.StatusBarUtil
import fule.com.playandroidkotlin.ui.util.StatusBarUtils
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 作者： njb
 * 时间： 2018/8/11 0011-下午 4:57
 * 描述： 主界面
 * 来源：
 */
class MainActivity : BaseActivity<BasePresenter<BaseView>>(){
    private var fmHome: HomeFragment? = null
    private var fmknow: KnowledgeFragment? = null
    private var fmNavigation: NavigationFragment? = null
    private var fmProject: ProjectFragment? = null
    private var fragmentManager: FragmentManager? = null
    private var mLastClickReturnTime: Long = 0

    override val layoutId: Int
        get() = R.layout.activity_main

    override fun createPresenter(): BasePresenter<BaseView> {
        return BasePresenter(this)
    }

    override fun setStatusBar() {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        StatusBarUtils.StatusBarLightMode(this, StatusBarUtils.StatusBarLightMode(this))
    }

    override fun initView() {
        initToolBar()
        initTab()
    }

    private fun initToolBar() {
        toolbar!!.run {
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(false)
        }
    }

    private fun initTab() {
        fmHome = HomeFragment()
        fmknow = KnowledgeFragment()
        fmNavigation = NavigationFragment()
        fmProject = ProjectFragment()
        fragmentManager = supportFragmentManager
        fragmentManager!!.beginTransaction()
                .add(R.id.ll_main, fmHome!!)
                .add(R.id.ll_main, fmknow!!)
                .add(R.id.ll_main, fmNavigation!!)
                .add(R.id.ll_main, fmProject!!).show(fmHome!!)
                .hide(fmknow!!).hide(fmknow!!)
                .hide(fmNavigation!!).commit()
        fmHome!!.userVisibleHint = true
    }


    override fun addListener() {

    }


    override fun showLoading() {
        showWaitDialog()
    }

    override fun hideLoading() {
        dismissWaitDialog()
    }

    @OnClick(R.id.rb_main_home, R.id.rb_main_know, R.id.rb_main_nav, R.id.rb_main_project,R.id.iv_search)
    fun onViewClicked(view: View) {
        val transaction = fragmentManager!!.beginTransaction()
        when (view.id) {
            //首页
            R.id.rb_main_home -> {
                transaction.show(fmHome!!).hide(fmknow!!).hide(fmNavigation!!).hide(fmProject!!).commit()
                setUnSelect(view.id)
                rb_main_home!!.isChecked = true
            }
            //知识
            R.id.rb_main_know -> {
                transaction.show(fmknow!!).hide(fmHome!!).hide(fmNavigation!!).hide(fmProject!!).commit()
                setUnSelect(view.id)
                rb_main_know!!.isChecked = true
            }
            //导航
            R.id.rb_main_nav -> {
                transaction.show(fmNavigation!!).hide(fmknow!!).hide(fmHome!!).hide(fmProject!!).commit()
                setUnSelect(view.id)
                rb_main_nav!!.isChecked = true
            }
      
            //项目
            R.id.rb_main_project -> {
                transaction.show(fmProject!!).hide(fmknow!!).hide(fmNavigation!!).hide(fmHome!!).commit()
                setUnSelect(view.id)
                rb_main_project!!.isChecked = true
            }
            R.id.iv_search ->{
                Intent(context, SearchActivity::class.java).run {
                    startActivity(this)
                }
            }
        }
    }
    
    override fun onBackPressed() {
        if (System.currentTimeMillis() - mLastClickReturnTime > 1000L) {
            mLastClickReturnTime = System.currentTimeMillis()
            showtoast("再按一次退出应用")
        } else {
            finish()
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        selectShop()
    }

    /**
     * 默认首页
     */
    private fun selectShop() {
        val transaction = fragmentManager!!.beginTransaction()
        transaction.show(fmHome!!).hide(fmknow!!).hide(fmNavigation!!).hide(fmProject!!).commit()
        fmHome!!.userVisibleHint = true
        setUnSelect(rb_main_home!!.id)
        rb_main_home!!.isChecked = true
        StatusBarUtils.StatusBarLightMode(this, StatusBarUtils.StatusBarLightMode(this))
    }

    /**
     * 设置 全部不选中
     */
    private fun setUnSelect(viewId: Int) {
        rb_main_home!!.isChecked = false
        rb_main_know!!.isChecked = false
        rb_main_nav!!.isChecked = false
        rb_main_project!!.isChecked = false

        when (viewId) {
            rb_main_nav!!.id -> {
                fmHome!!.userVisibleHint = false
                fmknow!!.userVisibleHint = false
                fmNavigation!!.userVisibleHint = true
                fmProject!!.userVisibleHint = false
            }
            rb_main_know!!.id -> {
                fmHome!!.userVisibleHint = false
                fmknow!!.userVisibleHint = true
                fmNavigation!!.userVisibleHint = false
                fmProject!!.userVisibleHint = false

            }
            rb_main_home!!.id -> {
                fmHome!!.userVisibleHint = true
                fmknow!!.userVisibleHint = false
                fmNavigation!!.userVisibleHint = false
                fmProject!!.userVisibleHint = false

            }
            rb_main_project!!.id -> {
                fmHome!!.userVisibleHint = false
                fmknow!!.userVisibleHint = false
                fmNavigation!!.userVisibleHint = false
                fmProject!!.userVisibleHint = true
            }
        }
    }
}



